package org.jackysoft.controller.base;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.UUID;

import org.apache.commons.io.IOUtils;
import org.jackysoft.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/Image")
public class ImageController {
	private String inputName;
	private InputStream imageStream;
	private String CKEditor;
	private String CKEditorFuncNum;
	private String langCode;
	
	protected File upload = null;
	protected String uploadFileName = null;
	protected String uploadContentType = null;
	private String imagePath;		
	
	public String ckimgupload() throws Exception{
		String appBase = System.getProperty("appBase");
		String filename = UUID.randomUUID().toString();
		String ext = StringUtils.splitPostFileName(this.uploadFileName);
		String target = String.format("%s%simages%s%s.%s", 
				appBase,
				File.separator,
				File.separator,
				filename,
				ext				
				);
		IOUtils.copy(new FileReader(upload), new BufferedOutputStream(new FileOutputStream(target)));
		String contextPath  = System.getProperty("contextPath");
		imagePath =String.format("%s%simages%s%s.%s", 
				contextPath,
				"/",
				"/",
				filename,
				ext				
				);
		return "ckimgupload";
	}

	public String getInputName() {
		return inputName;
	}

	public void setInputName(String inputName) {
		this.inputName = inputName;
	}

	public InputStream getImageStream() {
		return imageStream;
	}

	public void setImageStream(InputStream imageStream) {
		this.imageStream = imageStream;
	}
	
	public String getCKEditor() {
		return CKEditor;
	}

	public String getCKEditorFuncNum() {
		return CKEditorFuncNum;
	}

	public String getLangCode() {
		return langCode;
	}

	public void setCKEditor(String cKEditor) {
		CKEditor = cKEditor;
	}

	public void setCKEditorFuncNum(String cKEditorFuncNum) {
		CKEditorFuncNum = cKEditorFuncNum;
	}

	public void setLangCode(String langCode) {
		this.langCode = langCode;
	}

	public File getUpload() {
		return upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}
}
